package com.example.exercise.appmanage.system.controller;

import com.example.exercise.core.base.dto.BaseResponse;
import com.example.exercise.appmanage.system.dto.request.MenuRequest;
import com.example.exercise.appmanage.system.dto.response.MenuResponse;
import com.example.exercise.appmanage.system.mapper.entity.MenuEntity;
import com.example.exercise.appmanage.system.service.MenuService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("mg/menu")
//@Api(description = "菜单维护和查询")
public class SysMenuController {
	@Resource
	private MenuService menuService;

//	@ApiOperation(value = "获取菜单列表", notes = "所有菜单查询")
	@RequestMapping(value = "getMenuListAll", method = { RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE })
	public BaseResponse<List<MenuResponse>> getMenuListAll() {
		BaseResponse<List<MenuResponse>> baseResponse = new BaseResponse<>();
		baseResponse.ok(menuService.getMenuList());
		return baseResponse;
	}

//	@ApiOperation(value = "添加菜单", notes = "添加菜单")
	@RequestMapping(value = "saveMenu", method = { RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
	public BaseResponse<Object> saveMenu(@RequestBody MenuRequest menuRequest) {
		BaseResponse<Object> baseResponse = new BaseResponse<>();
		menuService.saveMenu(menuRequest);
		baseResponse.ok();
		return baseResponse;
	}

//	@ApiOperation(value = "根据id删除菜单", notes = "根据id删除菜单")
	@RequestMapping(value = "dropMenuById", method = { RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
	public BaseResponse<Object> dropMenuById(@RequestParam int id) {
		BaseResponse<Object> baseResponse = new BaseResponse<>();
		boolean bool = menuService.dropMenuById(id);
		if(bool){
			baseResponse.ok();
		}else{
			baseResponse.setCode(1);
			baseResponse.setMsg("有下一级菜单，不能删除");
		}
		return baseResponse;
	}

//	@ApiOperation(value = "修改菜单", notes = "修改菜单")
	@RequestMapping(value = "modifyMenu", method = { RequestMethod.PUT}, produces = {MediaType.APPLICATION_JSON_VALUE })
	public BaseResponse<Object> modifyMenu(@RequestBody MenuRequest menuRequest) {
		BaseResponse<Object> baseResponse = new BaseResponse<>();
		menuService.modifyMenu(menuRequest);
		baseResponse.ok();
		return baseResponse;
	}

//	@ApiOperation(value = "查询菜单详情", notes = "查询菜单详情")
	@RequestMapping(value = "findMenuById", method = { RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE })
	public BaseResponse<MenuEntity> findMenuById(@RequestParam int id) {
		BaseResponse<MenuEntity> baseResponse = new BaseResponse<>();
		MenuEntity menuEntity = menuService.findMenuById(id);
		if(menuEntity == null){
			baseResponse.setCode(-12);
			baseResponse.setMsg("数据为空");
		}else{
			baseResponse.ok(menuEntity);
		}
		return baseResponse;
	}

}
